package com.xbw.controller;



import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.codehaus.jackson.map.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xbw.bean.Bis_Shop;
import com.xbw.bean.Bis_shop_post;
import com.xbw.bean.Sys_Menu;
import com.xbw.bean.Sys_User;
import com.xbw.service.UserService;
import com.zsy.bean.Bis_Shop_User;

@Controller
@RequestMapping("user")
public class UserController {
    
	
	
	@Autowired
	private UserService use;
	//闂傚倷娴囧▔鏇㈠窗閹版澘鍑犲┑鐘宠壘缁狀垶鏌ｉ幋锝呅撻柡鍛倐閺岋繝宕掑Ο鐚存嫹閺嶎偓鎷峰顒傜Ш闁哄被鍔戦幃銏ゅ川婵犲嫪绱曢梻浣烘嚀閸ゆ牠骞忛敓锟�
	@RequestMapping("main")
	public String main(HttpSession sess,Model md) {
		 Sys_User user=(Sys_User)sess.getAttribute("sysuser");
		 List<Sys_Menu> list=use.getmenu(user.getUser_id());
         md.addAttribute("list", list);
		 return "index";
	} 
	   //  闂佽崵濮撮幖顐︽偪閸ヮ灐褰掑幢濞戞顦悗骞垮劚閹冲酣鏌婇柆宥嗙厽鐟滄垿顢氳缁辩偛顓奸崱鎰盎闂佹眹鍨归幉鈥愁浖閿燂拷
	@RequestMapping("qlogin")
	public String qlogin(Model model){
		model.addAttribute("error", 1);
		return "login2";
	}
	     //  闂佽崵濮撮幖顐︽偪閸ヮ灐褰掑幢濞戞顦悗骞垮劚閹冲酣鏌婇崡鐐╂闁圭偓鍓氶悡濂告煏閸℃妯�婵☆偄鍟撮弫鎾绘晸閿燂拷
		@RequestMapping("index")
		public String index(HttpSession sess,Model md) {
			 Bis_Shop_User shopuser=(Bis_Shop_User) sess.getAttribute("shopuser");
	         md.addAttribute("shopuser", shopuser);
	         if(shopuser.getUser_type().equals("1")){//  1 娴狅綀銆冩惔妞惧瘜閵嗗倹婀佹径姘嚋鎼存鎽�
	        	 List<Bis_Shop> bisshop=use.selectShopOwner(shopuser.getShop_user_id());
	        	 return "redirect:/finance/home";
	         }else{  //  2 娴狅綀銆冮崨妯轰紣
	        	//  闂傚倷绶￠崑鍛┍閾忚宕查柛鎰典簼閸犲棝鏌熼悜妯荤妞ゃ儱绻橀弻锛勪沪鐠囨彃濮ゅ銈嗘礃椤ㄥ﹪骞婇敐鍜佺叆闁告劑鍔庨ˇ锟�
				 List<Sys_Menu> menu=use.selectMenu(shopuser.getShop_user_id());
	        	 md.addAttribute("menu", menu);
	        	 // 閹碉拷閸︺劌绨甸柧锟�
	        	 Bis_Shop bisshop=use.selectShop(shopuser.getShop_id());
	        	 md.addAttribute("bisshop", bisshop);
	        	 //  閹碉拷鐏炵偠浜存担锟�
	        	 Bis_shop_post shopPost=use.selectshopPost(shopuser.getAccount());
	        	 md.addAttribute("shopPost", shopPost);
	        	 return "index2";
	         }
		}
		//   鎼存ぞ瀵岄悙鐟板毊鏉╂稑鍙嗘惔妤呮懙
		@RequestMapping("shopkeeper")
		public String shopkeeper(HttpSession sess,Model model,@RequestParam("shop_id")String shop_id) {
			System.out.println("shop_id"+shop_id);
			  //   閻ц缍嶆禍杞颁繆閹拷
			 Bis_Shop_User shopuser=(Bis_Shop_User) sess.getAttribute("shopuser");
			 model.addAttribute("shopuser", shopuser);
			 //   閼挎粌宕�
			 List<Sys_Menu> menu=use.selectMenu(shopuser.getShop_user_id());
			 model.addAttribute("menu", menu);
			 //  閸忚渹缍嬫惔妤呮懙
        	 Bis_Shop bisshop=use.selectShop(shop_id);
        	 model.addAttribute("bisshop", bisshop);
        	//  閹碉拷鐏炵偠浜存担锟�
        	 Bis_shop_post shopPost=use.selectshopPost(shopuser.getAccount());
        	 model.addAttribute("shopPost", shopPost);
        	 shopuser.setShop_id(shop_id);
        	 sess.setAttribute("shopuser",shopuser );
			return "index2";
		}
		
	//闂傚倷娴囧▔鏇㈠窗閹版澘鍑犲┑鐘宠壘缁狀垳锟界懓瀚竟鍡欑矓閿燂拷
	@RequestMapping("login")
	public String login(Model model,String account,String password,@RequestParam(value="stat",defaultValue="2")String stat,HttpSession sess){
		Subject subject=SecurityUtils.getSubject();
		String name=account+","+stat;
		UsernamePasswordToken  token=new UsernamePasswordToken(name, password);
		System.out.println(name+"  : "+password);
		    //   1 闂備礁鎼�氱兘宕归悧鍫熸珷闁绘顕ч惌妤呮煙濞堝灝鏋熸い蹇嬪灮閹插摜鏁钘夘伓
		if(stat.equals("1")){
			Bis_Shop_User shopuser=use.selectShopUser(account);
			sess.setAttribute("shopuser", shopuser);
			if(!subject.isAuthenticated()){
				try {
				subject.login(token);	
				} catch (Exception e) {
					System.out.println(e.getMessage());
					model.addAttribute("error", 2);
			        return "login2";
				}
			}
			System.out.println("闂傚倷绶￠崑鍛┍閾忚宕查柛鎰ㄦ櫓濞堢晫锟界櫢鎷烽柛鎰典簼椤拷111");
	        return "redirect:index";
		}else{
			if(account==null||account.equals("")||password==null||password.equals("")){
				model.addAttribute("error", 1);
		        return "login";
			}else{
				//  2   闂備礁鎼�氭悂骞忛敓锟� 闂備礁鎲￠懝鐐殽濮濆被浜归柟缁㈠枟閸庡矂鎮归崶顏勭毢缂佸鎷�
				Sys_User sysuser=use.getuser(account);
				sess.setAttribute("sysuser", sysuser);
				if(!subject.isAuthenticated()){
					try {
					subject.login(token);	
					} catch (Exception e) {
						System.out.println(e.getMessage());
						model.addAttribute("error", 2);
				        return "login";
					}
				}
			}
	        return "redirect:main";
		}
	}
	
	  //json闂傚倷娴囧▔鏇㈠窗閹版澘鍑犲┑鐘宠壘缁狀垶鏌ｉ幋锝呅撻柡鍛倐閺岋繝宕掑Ο鐚存嫹閺嶎偓鎷峰鐐
	 private void resjson(HttpServletResponse response,Object obj){
	    	ObjectMapper  om=new ObjectMapper();
	    	try {
				String json=om.writeValueAsString(obj);
				System.out.println(json);
				PrintWriter out=response.getWriter();
				out.write(json);
				out.flush();
				out.close();
				return;
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
	    }
	/* //闂備浇宕垫慨鎾箹椤愶附鍋柛銉亹瑜版帒骞㈡繛鎴烆焽椤︻偊姊洪崫鍕垫Ц闁诲繑绻堣矾闁告稑鐡ㄩ悡鏇熺箾閸℃ê濮囨い銉ヮ儐缁绘盯宕ㄩ銏犲Г闂侀潧娲﹂崝娆忣嚕閹绢喖鐒垫い鎺戯攻鐎氱懓顭块懜闈涘妞ゃ儱鐗撻弻銊╂偆閸屾稑顏�
	 @RequestMapping("indextwo")
		public String indextwo() {
			 return "index2";
		} */
	  //   闂佹彃绉堕悿鍡欙拷闈涙閻栵拷
	 @RequestMapping("resetpwd")
	 @ResponseBody
	 public String resetpwd(String name,String idno,@RequestParam(value="stat",defaultValue="2")String stat){
		 System.out.println(name+" : "+idno+" : "+stat);
		 Map<String,Object> map=new HashMap<>();
		 map.put("name",name);
		 map.put("idno", idno);
		 map.put("status", stat);
		 String result=use.resetpwd(map);
		 return result;
	 }
	 
}
